import { getPatientBaseObject } from '../../../utils/api/patientBase'
import userBaseInfoState from '../../../stores/userBaseInfo'
import { validateNull } from '../../../utils/util';

Component({

  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    active: 0,
    currentComponent: <string | null>'PersonalOverview',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(event: any) {
      this.setData({
        active: event.detail.index
      })
      this.getUserInfo();
    },
    updateSubCompoent(userInfo: PatientBaseEntity) {
      console.log(userInfo);
      // TODO: 更新子组件数据
      let subComponent1 = this.selectComponent('#personal-overview');
      let subComponent2 = this.selectComponent('#medical-condition');
      let subComponent3 = this.selectComponent('#medical-record');
      if (subComponent1) {
        subComponent1.updateUserInfo(userInfo);
      }
      if (subComponent2) {
        subComponent2.updateUserInfo(userInfo);
      }
      if (subComponent3) {
        subComponent3.updateUserInfo(userInfo);
      }
    },
    // 获取用户基本信息
    getUserInfo() {
      const data = <PatientBaseEntity>{ wxUid: wx.getStorageSync("openid") };
      // 如果已经获取状态信息那么无需再次获取，判断是否具有 patientUid和username即可确认
      getPatientBaseObject(data).then((res: any) => {
        console.log('获取基本信息成功-home-nav', res);
        userBaseInfoState.onChangeData(res.data[0])
        // 执行更新子组件数据
        this.updateSubCompoent(userBaseInfoState.data);
      }).catch((err: any) => {
        console.log("获取基本信息失败", err);
      })
    },
  },
  /**
 * 组件的生命周期方法
 */
  lifetimes: {
    ready: function () {
      this.getUserInfo()
    }
  },

  pageLifetimes: {
    show: function () {
      this.getUserInfo();
    }
  },

  options: {
    addGlobalClass: true
  },
})